<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="java.util.UUID"%>
<%@page import="Managers.Utilities.RequestResponseWrapper"%>
<%@page import="Utilities.ApplicationException"%>
<%@page import="Managers.Utilities.ResponseMessagesContainer"%>
<%@page import="Utilities.Functions"%>
<%@page import="org.virtualbox_3_2.DeviceType"%>
<%@page import="org.virtualbox_3_2.MediumType"%>
<%@page import="Utilities.Constants"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Beans.Requests.MediumManagement.CloneDiskRequest"%>
<%@page import="Beans.Requests.MediumManagement.MergeDisksRequest"%>
<%@page import="Beans.Requests.MediumManagement.ModifyMediumRequest"%>
<%@page import="Beans.Requests.MediumManagement.CompactDiskRequest"%>
<%@page import="Beans.Requests.MediumManagement.ResizeDiskRequest"%>
<%@page import="Beans.Requests.MediumManagement.ResetDiskRequest"%>
<%@page import="Beans.Requests.MediumManagement.RemoveMediumRequest"%>
<%@page import="Beans.Requests.MediumManagement.OpenFloppyDiskRequest"%>
<%@page import="Beans.Requests.MediumManagement.OpenDVDRequest"%>
<%@page import="org.virtualbox_3_2.AccessMode"%>
<%@page import="Beans.Requests.MediumManagement.OpenHardDiskRequest"%>
<%@page import="org.virtualbox_3_2.MediumVariant"%>
<%@page import="Beans.Requests.MediumManagement.CreateHardDiskRequest"%>
<%@page import="Beans.Requests.MediumManagement.MediumManagementRequest"%>
<%@page import="Managers.Utilities.User"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Beans.Responses.MediumManagementResponse"%>
<%@page import="Utilities.UIFunctions"%>
<%
    //retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);
    int mediumType = 0;

    //attempts to retreive the supplied request action
    int action = 0;
    try
    {
        action = Integer.parseInt(request.getParameter("action"));
    }
    catch(NumberFormatException e){}
    
    MediumManagementRequest mediumRequest = null;
    switch(action)
    {
        //create hdd request
        case 1:
        {
            mediumType = 1;
            
            int diskType = 0;
            try
            {
                diskType = Integer.parseInt(request.getParameter("create.disk.type"));
            }
            catch(NumberFormatException e){}

            if(diskType == 1) //normal hdd is to be created
            {
                long logicalDiskSize = 0;

                try
                {
                    logicalDiskSize = Long.parseLong(request.getParameter("create.disk.size"));
                }
                catch(NumberFormatException e){}

                mediumRequest = new CreateHardDiskRequest(userSessionData.getUserData().getUserId(),
                                                          userSessionData.getCurrentServerId(),
                                                          request.getParameter("create.disk.format"),
                                                          request.getParameter("create.disk.location"),
                                                          MediumVariant.valueOf(request.getParameter("create.disk.variant")),
                                                          logicalDiskSize,
                                                          request.getParameter("create.disk.permissions"));
            }
            else if(diskType == 2) //differencing hdd is to be created
            {
                mediumRequest = new CreateHardDiskRequest(userSessionData.getUserData().getUserId(),
                                                          userSessionData.getCurrentServerId(),
                                                          request.getParameter("create.disk.format"),
                                                          request.getParameter("create.disk.location"),
                                                          MediumVariant.valueOf(request.getParameter("create.disk.variant")),
                                                          request.getParameter("create.disk.permissions"),
                                                          request.getParameter("create.disk.parentId"));

            }
            else
                ;
        } break;

        //open hdd request
        case 2:
        {
            mediumType = 1;
            mediumRequest = new OpenHardDiskRequest(userSessionData.getUserData().getUserId(),
                                                    userSessionData.getCurrentServerId(),
                                                    request.getParameter("open.disk.location"),
                                                    AccessMode.valueOf(request.getParameter("open.disk.accessMode")),
                                                    request.getParameter("open.disk.permissions"));

            //according to the API, an empty String would be sufficient for
            //VirtualBox to generate a new UUID but it is not...
            ((OpenHardDiskRequest)mediumRequest).setImageId(UUID.randomUUID().toString());
        } break;

        //open dvd request
        case 3:
        {
            mediumType = 2;
            mediumRequest = new OpenDVDRequest(userSessionData.getUserData().getUserId(),
                                               userSessionData.getCurrentServerId(),
                                               request.getParameter("open.dvd.location"),
                                               request.getParameter("open.dvd.permissions"));
        } break;

        //open fdd request
        case 4:
        {
            mediumType = 3;
            mediumRequest = new OpenFloppyDiskRequest(userSessionData.getUserData().getUserId(),
                                                      userSessionData.getCurrentServerId(),
                                                      request.getParameter("open.fdd.location"),
                                                      request.getParameter("open.fdd.permissions"));
        } break;

        //modify medium request
        case 5:
        {
            //attempts to retrieve the medium device type
            DeviceType mediumDeviceType = null;
            try
            {
                int parameterMediumType = Integer.parseInt(request.getParameter("type"));
                mediumType = parameterMediumType;
                switch(parameterMediumType)
                {
                    case 1: mediumDeviceType = DeviceType.HARD_DISK; break;
                    case 2: mediumDeviceType = DeviceType.DVD; break;
                    case 3: mediumDeviceType = DeviceType.FLOPPY; break;
                    default: ;
                }
            }
            catch(NumberFormatException e)
            {}

            mediumRequest = new ModifyMediumRequest(userSessionData.getUserData().getUserId(),
                                                    userSessionData.getCurrentServerId(),
                                                    request.getParameter("id"),
                                                    mediumDeviceType);

            //completes the request build based on the supplied data
            
            if(request.getParameter("modify.medium.description.submit") != null)
                ((ModifyMediumRequest)mediumRequest).setDescription(request.getParameter("modify.medium.description"));
            else
                ;

            if(request.getParameter("modify.medium.location.submit") != null)
                ((ModifyMediumRequest)mediumRequest).setLocation(request.getParameter("modify.medium.location"));
            else
                ;

            if(request.getParameter("modify.medium.type.submit") != null)
                ((ModifyMediumRequest)mediumRequest).setType(MediumType.valueOf(request.getParameter("modify.medium.type")));
            else
                ;

            if(request.getParameter("modify.medium.autoreset.submit") != null)
                ((ModifyMediumRequest)mediumRequest).setAutoReset(request.getParameter("modify.medium.autoreset") != null);
            else
                ;

            if(request.getParameter("modify.medium.permissions.submit") != null)
                ((ModifyMediumRequest)mediumRequest).setPermissions(request.getParameter("modify.medium.permissions"));
            else
                ;

            if(request.getParameter("modify.medium.newOwner.submit") != null)
            {
                int newOwnerId = Constants.INVALID_USER_ID;

                try
                {
                    newOwnerId = Integer.parseInt(request.getParameter("modify.medium.newOwner"));
                }
                catch(NumberFormatException e){}

                ((ModifyMediumRequest)mediumRequest).setNewOwnerId(newOwnerId);
            }
            else
                ;
        } break;

        //merge disks request
        case 6:
        {
            mediumType = 1;
            mediumRequest = new MergeDisksRequest(userSessionData.getUserData().getUserId(),
                                                  userSessionData.getCurrentServerId(),
                                                  request.getParameter("id"),
                                                  request.getParameter("merge.disks.target"));
        } break;

        //clone disk request
        case 7:
        {
            String parentID = request.getParameter("clone.disk.parent");
            if(parentID.trim().equals(""))
                parentID = null;
            else
                ;

            mediumType = 1;
            mediumRequest = new CloneDiskRequest(userSessionData.getUserData().getUserId(),
                                                 userSessionData.getCurrentServerId(),
                                                 request.getParameter("id"),
                                                 request.getParameter("clone.disk.target"),
                                                 MediumVariant.valueOf(request.getParameter("clone.disk.variant")),
                                                 parentID);
        } break;

        //compact disk request
        case 8:
        {
            mediumType = 1;
            mediumRequest = new CompactDiskRequest(userSessionData.getUserData().getUserId(),
                                                   userSessionData.getCurrentServerId(),
                                                   request.getParameter("id"));
        } break;

        //resize disk request
        case 9:
        {
            mediumType = 1;
            long newDiskSize = 0;

            try
            {
                newDiskSize = Long.parseLong(request.getParameter("resize.disk.size"));
            }
            catch(NumberFormatException e){}

            mediumRequest = new ResizeDiskRequest(userSessionData.getUserData().getUserId(),
                                                  userSessionData.getCurrentServerId(),
                                                  request.getParameter("id"),
                                                  newDiskSize);
        } break;

        //reset disk request
        case 10:
        {
            mediumType = 1;
            mediumRequest = new ResetDiskRequest(userSessionData.getUserData().getUserId(),
                                                 userSessionData.getCurrentServerId(),
                                                 request.getParameter("id"));
        } break;

        //remove medium request
        case 11:
        {
            DeviceType mediumDeviceType = null;
            
            try
            {
                int parameterMediumType = Integer.parseInt(request.getParameter("type"));
                mediumType = parameterMediumType;
                switch(parameterMediumType)
                {
                    case 1: mediumDeviceType = DeviceType.HARD_DISK; break;
                    case 2: mediumDeviceType = DeviceType.DVD; break;
                    case 3: mediumDeviceType = DeviceType.FLOPPY; break;
                    default: ;
                }
            }
            catch(NumberFormatException e)
            {}

            boolean deleteStorage = (request.getParameter("delete") != null);

            mediumRequest = new RemoveMediumRequest(userSessionData.getUserData().getUserId(),
                                                    userSessionData.getCurrentServerId(),
                                                    request.getParameter("id"),
                                                    mediumDeviceType,
                                                    deleteStorage);
        } break;

        default: ;
    }

    //attempts to send the request and retrieves the response
    SendingRequestResult sendingResult = UIFunctions.sendServiceRequest(request, response, mediumRequest, Service.MEDIA_MANAGEMENT);
    if(sendingResult != SendingRequestResult.OK)
    {
        //retrieves the user's messages container
        ResponseMessagesContainer messagesContainer =
            (ResponseMessagesContainer)request.getSession(false).getAttribute(ParameterNames.SESSION_REQUEST_MESSAGES_OBJECT);

        int exceptionCode = UIFunctions.parseSendingRequestResult(sendingResult);

        //adds the response from the failed operation
        MediumManagementResponse mediumResponse = new MediumManagementResponse(false, new ApplicationException(exceptionCode));
        messagesContainer.put(new RequestResponseWrapper(mediumRequest, mediumResponse));
    }
    else
        ;

    //based on the requested action, the proper redirect page is selected
    if(Functions.isUUIDValid(request.getParameter("id")) && action != 11)
        response.sendRedirect("media.jsp?type=" + mediumType + "&id=" + request.getParameter("id"));
    else
        response.sendRedirect("media.jsp?type=" + mediumType);
%>
